﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.BLL;
using Grove.ORM;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;


public partial class Agency_BidProductList : System.Web.UI.Page
{
    public string name, outlookc, code, bidnum, TableName, strFields, strConditions, mKey, TableConditon,mKeyTrue;
    public int Pagesize, PageIndex, PageCount;
    public int projectid;

    //projectid = Request.form("projectid");
   


    protected void Page_Load(object sender, EventArgs e)
    {
        projectid = 1;
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            if (!IsPostBack)
            {
                //GetProduct();
                ProductCode.Text = "";
                BidNum.Text = "";
                ProductName.Text = "";
                Outlookc.Text = "";

                //Pagesize = 30;
                showGridView(1);
            }
        }
        else
        { 

        }
          

    }

    //    public void GetProduct()
    //    {

    //        string sql = @" SELECT top 10 ProductCode,ProductName,Outlookc,Medicinemodel,Unit,ProductId,0 as ProductNumber
    //                        FROM product
    //                        where productid in (select distinct productid from bid_project_product)";
    //        Db db = new Db();
    //        DataTable dt = db.DataTable(sql);
    //        GridView1.DataSource = dt.DefaultView;
    //        GridView1.DataBind();



    //    }



    /// <summary>
    /// 查询总共有多少页
    /// </summary>
    /// <returns></returns>
    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    /// <param name="PageIndex">转向第几页</param>

    public void showGridView(int PageIndex)
    {

        Pagesize = 30;
        TableName = "product,bid_project_product";
        TableConditon = "product.productid=bid_project_product.productid and projectid="+projectid;
        strFields = "ProductCode,ProductName,Outlookc,Medicinemodel,Unit,Product.ProductId,0 as BidNum";
        mKey = "product.productid";
        mKeyTrue = "productid";
        strConditions = "";
        strConditions = fctConditions();
        string sql;
        if (strConditions == "")
        {
            sql = "select count(*) from " + TableName + "  where " + TableConditon;
        }
        else
        {
            sql = "select count(*) from " + TableName + "  where " + strConditions + " and " + TableConditon;
        }
        Db Pagedb = new Db();
        DataTable PageTable = Pagedb.DataTable(sql);
        int RowCount = (int)PageTable.Rows[0][0];

        PageCount = (int)(RowCount / Pagesize);
        if (RowCount % Pagesize > 0)
            PageCount = PageCount + 1;

        LabRowCount.Text = RowCount.ToString();
        LabPageCount.Text = PageCount.ToString();
        LabPageIndex.Text = PageIndex.ToString();
        PageTable.Dispose();
        Pagedb.Close();

        sql = Pagex(PageIndex, Pagesize, TableName, mKey, strFields, strConditions, TableConditon, mKeyTrue);


        Pagedb = new Db();
        PageTable = Pagedb.DataTable(sql);
        GridView1.DataSource = PageTable;
        GridView1.DataBind();
        PageTable.Dispose();
        Pagedb.Close();

    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp;
        code = ProductCode.Text.ToString().Trim(); 
        bidnum = BidNum.Text.ToString().Trim();
        name = ProductName.Text.ToString().Trim();
        outlookc = Outlookc.Text.ToString().Trim();
        strtemp = "";
        if (code != "")
        {
            strtemp = strtemp + "ProductCode like '%" + code + "%' and";
        }
        if (bidnum != "")
        {
            strtemp = strtemp + "bidNum like '%" + bidnum + "%' and";

        }
        if (name != "")
        {
            strtemp = strtemp + " ProductName like '%" + name + "%' and";
        }
        if (outlookc != "")
        {
            strtemp = strtemp + " outlookc like '%" + outlookc + "%' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;

    }

    /// <summary>
    /// 分页函数
    /// </summary>
    /// <param name="PageIndex">要转到的页号</param>
    /// <param name="PageSize">每一页的记录条数</param>
    /// <param name="TableName">表名</param>
    /// <param name="mKey">主键名称</param>
    /// <param name="strFields">要显示的字段名</param>
    /// <param name="strConditions">查询条件不用带 where </param>
    /// <returns></returns>
    public string Pagex(int PageIndex, int PageSize, string TableName, string mKey, string strFields, string strConditions, string TableConditon, string mKeyTrue)
    {
        string sql;
        if (PageIndex == 1)
        {
            if (strConditions == "")
            {
                sql = "select top " + PageSize + "  " + strFields + " from  " + TableName + "  where " + TableConditon;
            }
            else
            {
                sql = "select top " + PageSize + "  " + strFields + " from  " + TableName + "  where " + strConditions + " and " + TableConditon;
            }
        }
        else
        {
            if (strConditions == "")
            {
                sql = "select top " + PageSize + "  " + strFields + " from  " + TableName + "  " + " where  " + TableConditon + " and " + mKey + ">(select max(  " + mKeyTrue + ") from (select top " + ((PageIndex - 1) * PageSize) + "  " + mKey + " from " +"  "+ TableName + "  " + " where " + TableConditon + "  " + " order by " + mKey+ ") tblTmp)  order by " + mKey;
            }
            else
            {
                sql = "select top " + PageSize + "  " + strFields + " from  " + TableName + "  " + " where  " + strConditions + "  " + "and  " + TableConditon + "  " + " and " + mKey + ">(select max(  " + mKeyTrue + ") from (select top " + ((PageIndex - 1) * PageSize) + "  " + mKey + " from  " + TableName + "   where " + strConditions + "  "+" and " + TableConditon + "  " + " order by " + mKey+") tblTmp)  order by " + mKey;
            }
        }
        return sql;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        PageIndex = 1;
        showGridView(PageIndex);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        PageIndex = Convert.ToInt32(LabPageIndex.Text);
        PageCount = Convert.ToInt32(LabPageCount.Text);

        if (PageIndex + 1 < PageCount)
        {
            PageIndex += 1;
        }
        else
        {
            PageIndex = PageCount;
        }
        showGridView(PageIndex);

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        PageIndex = Convert.ToInt32(LabPageIndex.Text);
        PageCount = Convert.ToInt32(LabPageCount.Text);
        if (PageIndex > 1)
        {
            PageIndex -= 1;
        }
        else
        {
            PageIndex = 1;
        }
        showGridView(PageIndex);


    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        PageIndex = Convert.ToInt32(LabPageCount.Text);
        showGridView(PageIndex);

    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        PageIndex = 1;
        showGridView(PageIndex);
    }
   
    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            return _AccountType;
        }
    }  
}
